package com.ebupt.migu.music.push.service;

import com.ebupt.migu.common.pojo.Topic;
import com.ebupt.migu.common.push.PushRequestBody;
import com.ebupt.migu.common.push.PushTopicBean;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * @author liuyangyang@e-byte.com
 * @description: TODO
 * @date 2024/8/15 15:40
 */
@Slf4j
@Service
public class KafkaProducerService {

    private final KafkaTemplate<String, PushTopicBean> kafkaTemplate;

    @Autowired
    public KafkaProducerService(KafkaTemplate<String, PushTopicBean> kafkaTemplate) {
        this.kafkaTemplate = kafkaTemplate;
    }

    public void sendMessage(String taskId, PushRequestBody body) {
        String requestId = UUID.randomUUID().toString();
        PushTopicBean bean = new PushTopicBean();
        bean.setRequestId(requestId);
        bean.setTaskId(taskId);
        bean.setBody(body);
        // 添加日志追踪requestId
        MDC.put(Topic.REQUEST_ID_KEY,requestId);
        log.info("push生产者body -> {}",body);
        kafkaTemplate.send("RFMENT_PUSH_TOPIC", bean);
        MDC.remove(Topic.REQUEST_ID_KEY);
    }
}
